JavaScript Array Methods - WittyWriter

JavaScript Array Methods

📘 Key Concepts and Definitions

🧮 Callback Function Syntax Pattern

Many modern array methods use a callback function that receives up to three arguments:

array.method((element, index, array) => {
  // ... your logic here, operating on the element
});

Example with Arrow Function: const doubled = [1, 2, 3].map(num => num * 2);

🛠️ Common Array Methods

Iteration Methods

MethodDescriptionReturns
forEach()Executes a callback for each element in the array.undefined
map()Creates a new array by calling a function on every element.New array
filter()Creates a new array with all elements that pass a test (return true).New array

Aggregation & Testing Methods

MethodDescriptionReturns
reduce()Executes a reducer function to produce a single output value.Single value
every()Tests if all elements in the array pass a test.Boolean
some()Tests if at least one element in the array passes a test.Boolean

Finding Methods

MethodDescriptionReturns
find()Returns the first element that passes a test.Element or undefined
findIndex()Returns the index of the first element that passes a test.Index or -1
includes()Determines whether an array includes a certain value.Boolean

Modification & Creation Methods

MethodDescriptionType
push() / pop()Adds/removes an element from the end of an array.Mutating
unshift() / shift()Adds/removes an element from the beginning of an array.Mutating
splice()Adds/removes elements from an array at a specified index.Mutating
sort()Sorts the elements of an array in place.Mutating
slice()Returns a shallow copy of a portion of an array into a new array.Non-Mutating
concat()Merges two or more arrays, returning a new array.Non-Mutating

🧭 Workflow: Choosing the Right Method

⌨️ Productivity Tips

📊 Mutating vs. Non-Mutating

Understanding this distinction is crucial for avoiding bugs.

Mutating Methods (Change Original Array)Non-Mutating Methods (Return New Array)
push(), pop(), shift(), unshift(), splice(), sort(), reverse(), fill() map(), filter(), slice(), concat(), reduce(), flat(), flatMap()

🧪 Example: Processing E-Commerce Orders

Given an array of order objects, find the total revenue from all completed orders placed by customers in the USA.

const orders = [
  { id: 1, country: 'USA', status: 'completed', revenue: 100 },
  { id: 2, country: 'CAN', status: 'completed', revenue: 150 },
  { id: 3, country: 'USA', status: 'pending', revenue: 80 },
  { id: 4, country: 'USA', status: 'completed', revenue: 200 },
];

const totalUSARevenue = orders
  .filter(order => order.country === 'USA' && order.status === 'completed') // 1. Get only completed US orders
  .map(order => order.revenue) // 2. Get an array of just their revenue values
  .reduce((sum, currentRevenue) => sum + currentRevenue, 0); // 3. Sum up the values

console.log(totalUSARevenue); // Output: 300

🧹 Troubleshooting Common Errors

📚 References and Further Reading

🍪 We use cookies to improve your experience. Learn more